﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Testcraft.Physics;
using System.Linq;
using TestcraftLibrary.Common;
using System.Collections;

namespace Testcraft.Scene
{
    class BlockOctree
    {
        private struct Cuboid
        {
            public byte blockCount;
            public BitArray blocks;
            public BoundingBox bounds;
            public Cuboid[, ,] children;
        }

        const byte BlockTreshold = 64;
        Cuboid root;
        public List<BlockDescriptorAdd> blocks;

        public BlockOctree(BoundingBox rootBounds)
        {
            this.blocks = new List<BlockDescriptorAdd>();
            this.root = new Cuboid();
            this.root.blockCount = 0;
            this.root.blocks = null;
            this.root.children = null;
        }

        public void AddBlock(short x, short y, short z)
        {
            
        }

        private void AddBlockInternal(short x, short y, short z, ref Cuboid cuboid)
        {
            if (cuboid.blockCount > BlockTreshold)
            {
                
            }
        }

        private void SplitCuboid(ref Cuboid cuboid)
        {
            Vector3 childSize = cuboid.bounds.Max - cuboid.bounds.Min;
            cuboid.children = new Cuboid[2, 2, 2];
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    for (int z = 0; z < 2; z++)
                    {
                        Vector3 min = cuboid.bounds.Min + new Vector3(x * childSize.X, y * childSize.Y, z * childSize.Z);
                        cuboid.children[x, y, z] = new Cuboid();
                        cuboid.children[x, y, z].bounds = new BoundingBox(min, min + childSize);
                        cuboid.children[x, y, z].blockCount = 0;
                        cuboid.children[x, y, z].blocks = null;
                        cuboid.children[x, y, z].children = null;
                    }
                }
            }

            for (int i = 0; i < cuboid.blockCount; i++)
            {

                //cuboid.blocks[((blockX * this.Height + blockY) * this.Width + blockZ)]
            }
        }



        //public void Build()
        //{
        //    this.root.children = null;
        //    this.root.blocks.Clear();
        //    this.root.blocks.AddRange(this.blocks);
        //    this.BuildCuboid(ref this.root);
        //}

        //private void BuildCuboid(ref Cuboid current)
        //{
        //    if (current.blocks.Count > BlockTreshold)
        //    {
        //        Vector3 quarterSize = (current.bounds.Max - current.bounds.Min) / 2;
        //        current.children = new Cuboid[2, 2, 2];
        //        for (int x = 0; x < 2; x++)
        //        {
        //            for (int y = 0; y < 2; y++)
        //            {
        //                for (int z = 0; z < 2; z++)
        //                {
        //                    Vector3 childrenPosition = current.bounds.Min + (quarterSize * new Vector3(x, y, z));
        //                    current.children[x, y, z] = new Cuboid();
        //                    current.children[x, y, z].blocks = new List<BlockDescriptorAdd>();
        //                    current.children[x, y, z].bounds = new BoundingBox(childrenPosition, childrenPosition + quarterSize);

        //                    for (int i = current.blocks.Count - 1; i >= 0; i--)
        //                    {
        //                        if (current.children[x, y, z].bounds.Min.X <= current.blocks[i].Location.X &&
        //                            current.children[x, y, z].bounds.Max.X > current.blocks[i].Location.X &&
        //                            current.children[x, y, z].bounds.Min.Y <= current.blocks[i].Location.Y &&
        //                            current.children[x, y, z].bounds.Min.Y > current.blocks[i].Location.Y &&
        //                            current.children[x, y, z].bounds.Min.Z <= current.blocks[i].Location.Z &&
        //                            current.children[x, y, z].bounds.Min.Z > current.blocks[i].Location.Z)
        //                        {
        //                            current.children[x, y, z].blocks.Add(current.blocks[i]);
        //                            current.blocks.RemoveAt(i);
        //                        }
        //                    }

        //                    this.BuildCuboid(ref current.children[x, y, z]);
        //                }
        //            }
        //        }
        //    }
        //}
        
    }
}
